package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.News;
import com.jyy.prefabricated.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 新闻
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 新增新闻
     * @param news
     * @return
     */
    @PostMapping("/news")
    public AjaxResult insert(@Valid @RequestBody News news){
        log.info("Rest request to insert news:{}",news);
        int id = newsService.insert(news);
        News result = newsService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新新闻
     * @param news
     * @return
     */
    @PutMapping("/news")
    public AjaxResult update(@Valid @RequestBody News news){
        log.info("Rest request to update news:{}",news);
        newsService.update(news);
        News result = newsService.selectById(news.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    @PutMapping("/news-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update news:{}",dto);
        newsService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @DeleteMapping("/news-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete news by id:{}",dto);
        int row = newsService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询新闻详情
     * @param id
     * @return
     */
    @GetMapping("/news/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find news by id:{}",id);
        News result = newsService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除新闻
     * @param id
     * @return
     */
    @DeleteMapping("/news/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete news by id:{}",id);
        int row = newsService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询上一条新闻
     * @param id
     * @return
     */
    @GetMapping("/news-prev/{id}")
    public AjaxResult selectByPrev(@PathVariable Integer id){
        log.info("Rest request to find news by prev id:{}",id);
        List<News> result = newsService.selectByPage(id,"prev");
        return AjaxResult.success(result);
    }

    /**
     * 查询下一条新闻
     * @param id
     * @return
     */
    @GetMapping("/news-next/{id}")
    public AjaxResult selectByNext(@PathVariable Integer id){
        log.info("Rest request to find news by next id:{}",id);
        List<News> result = newsService.selectByPage(id,"next");
        return AjaxResult.success(result);
    }

    /**
     * 分页查询新闻
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @param top 是否置顶
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/news")
    public AjaxResult selectByNext(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status,@RequestParam(required = false) Boolean top, Pageable pageable){
        log.info("Rest request to find news by params categoryId:{}, title:{}, status:{},top:{}",categoryId, title, status,top);
        List<Integer> categoryIds = newsService.selectDeepCategoryIds(categoryId);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<News> news = newsService.selectDeepByParams(categoryIds, title, status, top);
        PageInfo<News> result = new PageInfo<>(news);
        return AjaxResult.success(result);
    }

    /**
     * 查询新闻
     * @param categoryId 分类id
     * @param title 标题
     * @param status 状态
     * @param top 是否置顶
     * @return
     */
    @GetMapping("/news-list")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false) Integer status,@RequestParam(required = false) Boolean top){
        log.info("Rest request to find news by params categoryId:{}, title:{}, status:{},top:{}",categoryId, title, status,top);
        List<Integer> categoryIds = newsService.selectDeepCategoryIds(categoryId);
        List<News> result = newsService.selectDeepByParams(categoryIds, title, status, top);
        return AjaxResult.success(result);
    }
}
